﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class signin : System.Web.UI.Page
{
    private string txtEmail, txtPass, txtCPass, txtName, txtAddress, txtCity, txtCountry, txtPhone, txtDescription;
    private string txtBirthday, txtAvatar;
    private SQLQuery sqlQuery = null;
    private string TABLE_CANDIDATE = "Candidate";
    private string path = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["candidate"] != null)
        {
            Response.Redirect("index.aspx");
        }
        sqlQuery = new SQLQuery();
        path = Server.MapPath(".") + "//image//avatar//candidate//";
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        if (getText())
        {
            if (sqlQuery.selectEmailIsExits(txtEmail, TABLE_CANDIDATE))
            {
                lbl_msg.Text = LocalVariables.EMAIL_BEEN_USED;
                return;
            }
            else
            {
                LocalVariables.SAVE_VALUE_AVATAR = "";
                if (file_upload.HasFile)
                {
                    LocalVariables.SAVE_VALUE_AVATAR = LocalVariables.getFileName("", file_upload.FileName);                    
                }
                if (sqlQuery.insertTableCandidate(txtEmail, txtPass, txtName, txtBirthday, txtAddress, txtCity, txtCountry, txtDescription, txtPhone, LocalVariables.SAVE_VALUE_AVATAR))
                {
                    if (file_upload.HasFile)
                    {
                        file_upload.SaveAs(path + LocalVariables.SAVE_VALUE_AVATAR);
                    }
                    layout_register.Visible = false;
                    layout_success.Visible = true;
                }
            }
        }
    }

    protected void btn_reset_Click(object sender, EventArgs e)
    {
        clearText();
    }

    private bool getText()
    {
        txtEmail = txt_email.Text.Trim();
        txtPass = txt_password.Text.Trim();
        if (txtPass.Length > 0)
        {
            if (txtPass.Length < 6)
            {
                lbl_msg.Text = LocalVariables.PASSWORD_CHARACTERS;
                return false;
            }
            txtCPass = txt_cpassword.Text.ToString().Trim();
            if (txtPass != txtCPass)
            {
                lbl_msg.Text = LocalVariables.CPASSWORD_FAILURE;
                return false;
            }
        }
        txtName = txt_name.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtName))
        {
            lbl_msg.Text = LocalVariables.NAME_FAILURE;
            return false;
        }
        txtAddress = txt_address.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtAddress))
        {
            lbl_msg.Text = LocalVariables.ADDRESS_FAILURE;
            return false;
        }
        txtCity = txt_city.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtCity))
        {
            lbl_msg.Text = LocalVariables.CITY_FAILURE;
            return false;
        }
        txtCountry = txt_country.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtCountry))
        {
            lbl_msg.Text = LocalVariables.COUNTRY_FAILURE;
            return false;
        }
        txtPhone = txt_phone.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtPhone))
        {
            txtPhone = "0";
        }
        txtDescription = ftb_description.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtDescription))
        {
            lbl_msg.Text = LocalVariables.DESCRIP_FAILURE;
            return false;
        }
        txtBirthday = bdp_birthday.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtBirthday))
        {
            lbl_msg.Text = LocalVariables.BIRTHDAY_FAILURE;
            return false;
        }
        return true;
    }

    private void clearText()
    {
        txt_email.Text = "";
        txt_password.Text = "";
        txt_cpassword.Text = "";
        txt_name.Text = "";
        txt_address.Text = "";
        txt_city.Text = "";
        txt_country.Text = "";
        txt_phone.Text = "";
        ftb_description.Text = "";
        bdp_birthday.SelectedValue = "";
    }
}